//                       .::::.
//                     .::::::::.
//                    :::::::::::
//                 ..:::::::::::'
//              '::::::::::::'
//                .::::::::::
//           '::::::::::::::..
//                ..::::::::::::.
//              ``::::::::::::::::
//               ::::``:::::::::'        .:::.
//              ::::'   ':::::'       .::::::::.
//            .::::'      ::::     .:::::::'::::.
//           .:::'       :::::  .:::::::::' ':::::.
//          .::'        :::::.:::::::::'      ':::::.
//         .::'         ::::::::::::::'         ``::::.
//     ...:::           ::::::::::::'              ``::.
//    ```` ':.          ':::::::::'                  ::::..
//                       '.:::::'                    ':'````..
package com.xw.service;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.xw.dao.*;
import com.xw.model.Banner;
import com.xw.model.BasicSet;
import com.xw.model.Videos;
import com.xw.util.AppUtil;
import com.xw.util.JsonData;
import com.xw.vo.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class BannerServiceImpl implements BannerService {

    @Resource
    private BannerMapper bannerMapper;
    @Resource
    private SysTypeMapper typeMapper;
    @Resource
    private VideosMapper videosMapper;
    @Resource
    private NewsMapper newsMapper;
    @Resource
    private PlayRecordMapper playRecordMapper;
    @Resource
    private BasicSetMapper setMapper;
    @Resource
    private SysUserInfoMapper userInfoMapper;

    @Override
    public Map findHomeSearchMP(String userId, String searchName, Integer type, Integer page, Integer limit) {
        Map map = Maps.newHashMap();
        if (page != null && limit != null){
            PageHelper.startPage(page,limit,false);
        }
        switch (type){
            case 1:
                //课程
                List<VideosVo> videosList = videosMapper.findVideoBySearchName(searchName);
                map.put("videosList",videosList);
                break;
            case 2:
                //导师
                List<TeacherVo> teacherVoList =  userInfoMapper.findTeacher(searchName);
                map.put("teacherVoList",teacherVoList);
                break;
            default:
                break;
        }

        return JsonData.success(map);
    }

    @Override
    public Map findHomeMP(String userId) {
        Map map = Maps.newHashMap();
        //分类
        List<TypeVo> typeList = typeMapper.findTypeOne();
        for (TypeVo type : typeList){
            List<TypeVo> voList = typeMapper.findTypeByStFid(type.getStId());
            type.setList(voList);
            for (TypeVo typeVo : voList) {
                List<TypeVo> voList1 = typeMapper.findTypeByStFid(typeVo.getStId());
                typeVo.setList(voList1);
            }
        }

        //轮播图 3条
        PageHelper.startPage(1,3,false);
        PageHelper.orderBy("sort asc");
        Banner banner = new Banner();
        banner.setUpOrDown(1);
        List<Banner> bannerList = bannerMapper.select(banner);

        //推荐课程（后台配置条数）
        BasicSet set = setMapper.selectAll().get(0);
        PageHelper.startPage(1,set.getRecommendVideoCount(),false);
        List<VideosVo> recommendVideosList = videosMapper.recommentVideos();

        if (StringUtils.isNotBlank(userId)) {
            //观看历史
            PageHelper.startPage(1,2,false);
            List<PlayRecordSonVo> playRecordSonList = playRecordMapper.findPlayRecordSonByUserId(userId);
            map.put("playRecordVoList",playRecordSonList);
        }
        map.put("typeList",typeList);
        map.put("bannerList",bannerList);
        map.put("recommendVideosList",recommendVideosList);

        return JsonData.success(map);
    }

    @Override
    public Map findHome() {
        Map map = Maps.newHashMap();

        //分类
        List<TypeVo> typeList = typeMapper.findTypeOne();
        for (TypeVo type : typeList){
            List<TypeVo> voList = typeMapper.findTypeByStFid(type.getStId());
            type.setList(voList);
            for (TypeVo typeVo : voList) {
                List<TypeVo> voList1 = typeMapper.findTypeByStFid(typeVo.getStId());
                typeVo.setList(voList1);
            }
        }

        //轮播图 3条
        PageHelper.startPage(1,3,false);
        PageHelper.orderBy("sort asc");
        Banner banner = new Banner();
        banner.setUpOrDown(1);
        List<Banner> bannerList = bannerMapper.select(banner);

        //推荐课程（6条）
        PageHelper.startPage(1,6,false);
        List<VideosVo> recommentVideosList = videosMapper.recommentVideos();

        //热榜课程 月销榜（10条）  周销榜（10条）（过去7天数据）
        String curMonth = AppUtil.dateStr(0, new Date());
        //当月销量最高 10 条
        PageHelper.startPage(1,10,false);
        List<VideosVo> curMonthList =  videosMapper.findTopSaleCountCurMonth(curMonth);

        //周销榜（10条）（过去7天数据）
        String curWeek =  AppUtil.calDate(3, -7);
        PageHelper.startPage(1,10,false);
        List<VideosVo> curWeekList =  videosMapper.findTopSaleCountCurWeek(curWeek);
        //新闻中心 4条
        PageHelper.startPage(1,4,false);
        List<NewsVo> newsList = newsMapper.findNews();

        map.put("typeList",typeList);
        map.put("bannerList",bannerList);
        map.put("recommentVideosList",recommentVideosList);
        map.put("curMonthList",curMonthList);
        map.put("curWeekList",curWeekList);
        map.put("newsList",newsList);

        return JsonData.success(map);
    }

    @Override
    public Map findBanner(Integer page, Integer limit) {
        if (page !=null && limit !=null){
            PageHelper.startPage(page,limit,false);
            PageHelper.orderBy("sort asc");
        }

        Banner banner = new Banner();
        banner.setUpOrDown(1);
        List<Banner> bannerList = bannerMapper.select(banner);

        return JsonData.success(bannerList);
    }

    @Override
    public Map del(String id) {
        int flag = bannerMapper.deleteByPrimaryKey(id);
        return JsonData.returnMap(flag);
    }


    @Override
    public Map edit(Banner banner) {
        banner.setUpdateTime(new Date());
        int flag = bannerMapper.updateByPrimaryKeySelective(banner);
        return JsonData.returnMap(flag);
    }

    @Override
    public Banner findByKeyId(String bannerId) {
        return bannerMapper.selectByPrimaryKey(bannerId);
    }

    @Override
    public Map add(Banner banner) {
        banner.setBannerId(AppUtil.uuid());
        banner.setCreateTime(new Date());
        int flag = bannerMapper.insertSelective(banner);
        return JsonData.returnMap(flag);
    }

    @Override
    public List<Banner> findByWhere() {
        PageHelper.orderBy("sort");
        List<Banner> list = bannerMapper.findByWhere();
        return list;
    }
}
